package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.doris.NoOwnerTableInfo;
import com.hllxd.hism.star.basic.service.service.NoOwnerTableInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * @author wuguangxi
 * @description
 * @date 2023/9/15 11:16
 **/
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/noOwnerTableInfo")
public class NoOwnerTableInfoController2 {

    @Autowired
    private NoOwnerTableInfoService service;

    @RestGrpcMapping("/list.do")
    public NTSTATUS listHealthScoreAppid(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            NoOwnerTableInfo tableInfo = params.driverObject(NoOwnerTableInfo.class);
            List<NoOwnerTableInfo> items = service.findByParams(tableInfo);
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    @RestGrpcMapping("/addPermitList.do")
    public NTSTATUS addPermitList(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Map<String, String> map = params.driverObject(Map.class);
            Long id = service.add2PermitList(Integer.parseInt(map.get("tableId")), map.get("tableName"));
            result.setData(id);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/removePermitList.do")
    public NTSTATUS removePermitList(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Map<String, String> map = params.driverObject(Map.class);
            service.removePermitList(Long.parseLong(map.get("id")));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
}
